package com.wjw.service;

import com.wjw.mapper.RoadwayTypeTableMapper;
import com.wjw.model.Roadway_type_table;
import com.wjw.utils.GetSqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车行道分类表数据库操作:逻辑控制层
 */
@Service
public class RoadwayTypeTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);


    public List<Roadway_type_table> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);
        try {
            List<Roadway_type_table> all = mapper.findAll();
            return all;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Roadway_type_table> findByPage(int page, int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Roadway_type_table> all = mapper.findByPage(start,limit);
            return all;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Roadway_type_table roadway_type_table){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);
        try {
            mapper.add(roadway_type_table);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Roadway_type_table roadway_type_table){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTypeTableMapper mapper=sqlsession.getMapper(RoadwayTypeTableMapper.class);
        try {
           mapper.change(roadway_type_table);
           sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
